package com.app.asktom;

import java.net.URL;
import java.util.ArrayList;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

import com.app.asktom.component.ImageLoaderGoogleMap;
import com.app.asktom.component.JsonHelper;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapActivity extends Activity implements OnMapClickListener, OnMapLongClickListener {
	static final LatLng HAMBURG = new LatLng(53.558, 9.927);
	static final LatLng KIEL = new LatLng(53.551, 9.993);
	private GoogleMap googleMap;
	public Bitmap bmp;
	private String response;
	private ArrayList<Map<String,String>> jsonResponse = new ArrayList<Map<String,String>>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		
		Intent intent = getIntent();
		this.response = intent.getExtras().getString("response");
		
		jsonResponse = 
				JsonHelper.getInstance()
				.responseSearchFood(this.response);
		
		
		
		try {
			initilizeMap();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

    /**
     * function to load map. If map is not created it will create it for you
     * */
    @SuppressLint("NewApi")
	private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
 
            googleMap.setOnMapClickListener(this);
            googleMap.setOnMapLongClickListener(this);
            
//            Marker hamburg = googleMap.addMarker(new MarkerOptions().position(HAMBURG)
//                    .title("Hamburg"));
            
//            Bitmap mIcon11 = null;
//            try {
//              InputStream in = new java.net.URL("http://www.gic.gov.lk/gic/org/Person_icon.png").openStream();
//              mIcon11 = BitmapFactory.decodeStream(in);
//            } catch (Exception e) {
////                Log.e("Error", e.getMessage());
//                e.printStackTrace();
//            }
//            BitmapDescriptorFactory.fromBitmap(mIcon11);
            
//            ImageLoaderExt imageLoader = new ImageLoaderExt(this);
//            Bitmap bitmap = imageLoader.getLoadedImage("http://www.gic.gov.lk/gic/org/Person_icon.png");
            
            
//            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
//            Bitmap bmp = Bitmap.createBitmap(80, 80, conf);
//            Canvas canvas1 = new Canvas(bmp);

            // paint defines the text color,
            // stroke width, size
//            Paint color = new Paint();
//            color.setTextSize(35);
//            color.setColor(Color.BLACK);
//
//            //modify canvas
//            canvas1.drawBitmap(bmp, 0,0, color);
//            canvas1.drawBitmap(BitmapFactory.decodeResource(getResources(),
//            	    R.drawable.ic_launcher), 0,0, color);            
//            canvas1.drawText("User Name!", 30, 40, color);            
//            
//
//            ImageLoaderCanvas loader = new ImageLoaderCanvas(this);
//            loader.DisplayImage("http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png", canvas1);
            
//            TheTask task = new TheTask();
//            task.execute();
            
            ImageLoaderGoogleMap imageLoaderGoogleMap = new ImageLoaderGoogleMap(this);
            
            
            
            for(int i = 0; i < this.jsonResponse.size(); i++) {
            	Double x = Double.parseDouble(this.jsonResponse.get(i).get("xCoordinate"));
            	Double y = Double.parseDouble(this.jsonResponse.get(i).get("yCoordinate"));
            	LatLng position = new LatLng(x, y);
            	imageLoaderGoogleMap.DisplayImage(this.jsonResponse.get(i).get("S3picloc"), 
            			googleMap, 
            			position, 
            			this.jsonResponse.get(i).get("S3name"));
            }
            
//            imageLoaderGoogleMap.DisplayImage("https://wbtom.s3.amazonaws.com/u106/ibdcb66d7a64f47ef1dbce07a663a72f7.png", googleMap, HAMBURG, "TESTING");
            
//            Marker kiel = googleMap.addMarker(new MarkerOptions()
//            .position(KIEL)
//            .title("Kiel")
//            .snippet("Kiel is cool")
////            .icon(BitmapDescriptorFactory.fromBitmap(bmp)).anchor(0.5f, 1));
//            .icon(BitmapDescriptorFactory
//                .fromResource(R.drawable.ic_launcher)));
            
            // Move the camera instantly to hamburg with a zoom of 15.
//            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

            // Zoom in, animating the camera.
//            googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
            
//            googleMap.getUiSettings().setMyLocationButtonEnabled(true);
            
//            googleMap.setMyLocationEnabled(true);
            
            
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.map, menu);
		return true;
	}

	@Override
	public void onMapLongClick(LatLng arg0) {
		Toast.makeText(this, arg0.latitude + " " + arg0.longitude ,Toast.LENGTH_LONG)
		.show();		
	}

	@Override
	public void onMapClick(LatLng arg0) {
		Toast.makeText(this, arg0.latitude + " " + arg0.longitude ,Toast.LENGTH_LONG)
		.show();
		
	}
	
//	 class TheTask extends AsyncTask <Void,Void,Void>
//	 {
//	@Override
//	protected void onPreExecute() {
//	    // TODO Auto-generated method stub
//	    super.onPreExecute();
//
//	}
//	@Override
//	protected Void doInBackground(Void... params) {
//	    URL url ;
//	    try {
//	    	String image = "https://wbtom.s3.amazonaws.com/u106/ibdcb66d7a64f47ef1dbce07a663a72f7.png";
////	    	String image = "http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png";
//	    url = new URL(image);
//	    bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
//	    } catch (Exception e) {
//	      e.printStackTrace();
//	         }
//	    return null;
//	}
//		@Override
//		protected void onPostExecute(Void result) {
//
//			super.onPostExecute(result);
//			if(bmp != null) {
//				Marker source = googleMap.addMarker(new MarkerOptions()
//				.position(HAMBURG)
//				.title("MyHome")
//				.snippet("Bangalore")  
//				.icon(BitmapDescriptorFactory.fromBitmap(bmp)));
////				.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))); 				
//			}
////			Marker source = googleMap.addMarker(new MarkerOptions()
////			.position(HAMBURG)
////			.title("MyHome")
////			.snippet("Bangalore")  
//////			.icon(BitmapDescriptorFactory.fromBitmap(bmp)));
////			.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));                                       
//	    }
//	}	
}
